#include "Query_Preprocessor.h"
#include <vector>
#include <string>
#include <iostream>
#include <boost/algorithm/string.hpp>
#include <stdio.h>
Query_Preprocessor::Query_Preprocessor()
{
	root = new QueryNode();
	root->level = 0;
	curr = root;
};

void Query_Preprocessor::createMapping(int variableType, vector<string>& variableName, struct Result& result)
{
	vector <int> tempResult;
	entityTableMapping[variableType]=variableName;
	for(int j=0;j<variableName.size();j++)
	{
		result.resultVector[variableType].resultEntity.push_back(tempResult);
	}

}
void Query_Preprocessor::createEntityTable()
{
	entityTable.push_back("assign");
	entityTable.push_back("call");
	entityTable.push_back("if");
	entityTable.push_back("while");
	entityTable.push_back("procedure");
	entityTable.push_back("stmtLst");
	entityTable.push_back("stmt");
	entityTable.push_back("variable");
	entityTable.push_back("constant");
	entityTable.push_back("INTEGER");
	entityTable.push_back("Synonyms");
	vector<string> init;


	init.push_back("0");

	for(int i =0;i<entityTable.size();i++)
	{
		entityTableMapping.push_back(init);
	}

	for(int i =0;i<entityTable.size();i++)
	{
		attrName.push_back(" ");
	}
	attrName[_VARIABLE] = "varName";
	attrName[_STMT] = "stmt#";
	attrName[_PROCEDURE] = "procName";
	attrName[_CONSTANT] = "value";



	/*for(int i=0;i<entityTable.size();i++)
	{
		cout <<i<<" "<<entityTable[i]<<endl;
	}*/

};
void Query_Preprocessor::createEntityRelTable()
{

	RelationshipTable.push_back("Follows");
	RelationshipTable.push_back("Follows*");
	RelationshipTable.push_back("Modifies");
	RelationshipTable.push_back("Uses");
	RelationshipTable.push_back("Parent");
	RelationshipTable.push_back("Parent*");
	RelationshipTable.push_back("Calls");
	RelationshipTable.push_back("Calls*");
	RelationshipTable.push_back("Next");
	RelationshipTable.push_back("Next*");


	NoOfArguments.push_back(2);
	NoOfArguments.push_back(2);
	NoOfArguments.push_back(2);
	NoOfArguments.push_back(2);
	NoOfArguments.push_back(2);
	NoOfArguments.push_back(2);
	NoOfArguments.push_back(2);
	NoOfArguments.push_back(2);
	NoOfArguments.push_back(2);
	NoOfArguments.push_back(2);

	//Follows Arguments
	
	arg1Follows.push_back(_ASSIGN);
	arg1Follows.push_back(_CALL);
	arg1Follows.push_back(_IF);
	arg1Follows.push_back(_WHILE);
	arg1Follows.push_back(_STMT);
	arg1Follows.push_back(_INTEGER);
	
	
	
	arg2Follows.push_back(_ASSIGN);
	arg2Follows.push_back(_CALL);
	arg2Follows.push_back(_IF);
	arg2Follows.push_back(_WHILE);
	arg2Follows.push_back(_STMT);
	arg2Follows.push_back(_INTEGER);
	






	// Follows* Arguments
	
	arg1FollowsT.push_back(_ASSIGN);
	arg1FollowsT.push_back(_CALL);
	arg1FollowsT.push_back(_IF);
	arg1FollowsT.push_back(_WHILE);
	arg1FollowsT.push_back(_STMT);
	arg1FollowsT.push_back(_INTEGER);
	

	arg2FollowsT.push_back(_ASSIGN);
	arg2FollowsT.push_back(_CALL);
	arg2FollowsT.push_back(_IF);
	arg2FollowsT.push_back(_WHILE);
	arg2FollowsT.push_back(_STMT);
	arg2FollowsT.push_back(_INTEGER);
	
	//Parent Arguments

	
	arg1Parent.push_back(_ASSIGN);
	arg1Parent.push_back(_CALL);
	arg1Parent.push_back(_IF);
	arg1Parent.push_back(_WHILE);
	arg1Parent.push_back(_STMT);
	arg1Parent.push_back(_INTEGER);
	
	
	arg2Parent.push_back(_ASSIGN);
	arg2Parent.push_back(_CALL);
	arg2Parent.push_back(_IF);
	arg2Parent.push_back(_WHILE);
	arg2Parent.push_back(_STMT);
	arg2Parent.push_back(_INTEGER);
	






	// Parent* Arguments
	
	arg1ParentT.push_back(_ASSIGN);
	arg1ParentT.push_back(_CALL);
	arg1ParentT.push_back(_IF);
	arg1ParentT.push_back(_WHILE);
	arg1ParentT.push_back(_STMT);
	arg1ParentT.push_back(_INTEGER);
	
	
	arg2ParentT.push_back(_ASSIGN);
	arg2ParentT.push_back(_CALL);
	arg2ParentT.push_back(_IF);
	arg2ParentT.push_back(_WHILE);
	arg2ParentT.push_back(_STMT);
	arg2ParentT.push_back(_INTEGER);
	

	// Modifies Arguments
	
	arg1Modifies.push_back(_ASSIGN);
	arg1Modifies.push_back(_CALL);
	arg1Modifies.push_back(_IF);
	arg1Modifies.push_back(_WHILE);
	arg1Modifies.push_back(_PROCEDURE);
	arg1Modifies.push_back(_STMT);
	arg1Modifies.push_back(_INTEGER);
	

	arg2Modifies.push_back(_VARIABLE);




	//Uses Arguments
	
	arg1Uses.push_back(_ASSIGN);
	arg1Uses.push_back(_CALL);
	arg1Uses.push_back(_IF);
	arg1Uses.push_back(_WHILE);
	arg1Uses.push_back(_PROCEDURE);
	arg1Uses.push_back(_STMT);
	arg1Uses.push_back(_INTEGER);

	
	arg2Uses.push_back(_VARIABLE);


	//Calls Argument

	arg1Call.push_back(_PROCEDURE);



	arg2Call.push_back(_PROCEDURE);





	//Calls* Arguments
	arg1CallT.push_back(_PROCEDURE);



	arg2CallT.push_back(_PROCEDURE);

	//Next Arguments
	arg1Next.push_back(_ASSIGN);
	arg1Next.push_back(_CALL);
	arg1Next.push_back(_IF);
	arg1Next.push_back(_WHILE);
	arg1Next.push_back(_STMT);
	arg1Next.push_back(_INTEGER);
	
	
	arg2Next.push_back(_ASSIGN);
	arg2Next.push_back(_CALL);
	arg2Next.push_back(_IF);
	arg2Next.push_back(_WHILE);
	arg2Next.push_back(_STMT);
	arg2Next.push_back(_INTEGER);

	//NextT Arguments
	arg1NextT.push_back(_ASSIGN);
	arg1NextT.push_back(_CALL);
	arg1NextT.push_back(_IF);
	arg1NextT.push_back(_WHILE);
	arg1NextT.push_back(_STMT);
	arg1NextT.push_back(_INTEGER);
	
	
	arg2NextT.push_back(_ASSIGN);
	arg2NextT.push_back(_CALL);
	arg2NextT.push_back(_IF);
	arg2NextT.push_back(_WHILE);
	arg2NextT.push_back(_STMT);
	arg2NextT.push_back(_INTEGER);


};
bool Query_Preprocessor::checkRelationshipArgumentsValidity(int arg1Index,int arg2Index,int relationshipIndex)
{

	if(relationshipIndex==0)
	{
		if((binarySearch(arg1Follows,0,(arg1Follows.size()),arg1Index)>=0) && (binarySearch(arg2Follows,0,(arg2Follows.size()),arg2Index)>=0))
		{
			return true;
		}
	}
	if(relationshipIndex==1)
	{
		if((binarySearch(arg1FollowsT,0,(arg1FollowsT.size()),arg1Index)>=0) && (binarySearch(arg2FollowsT,0,(arg2FollowsT.size()),arg2Index)>=0))
		{
			return true;
		}
	}
	if(relationshipIndex==2)
	{
		if((binarySearch(arg1Modifies,0,(arg1Modifies.size()),arg1Index)>=0) && (binarySearch(arg2Modifies,0,(arg2Modifies.size()),arg2Index)>=0))
		{
			return true;
		}
	}
	if(relationshipIndex==3)
	{
		if((binarySearch(arg1Uses,0,(arg1Uses.size()),arg1Index)>=0) && (binarySearch(arg2Uses,0,(arg2Uses.size()),arg2Index)>=0))
		{
			return true;
		}
	}
	if(relationshipIndex==4)
	{
		if((binarySearch(arg1Parent,0,(arg1Parent.size()),arg1Index)>=0) && (binarySearch(arg2Parent,0,(arg2Parent.size()),arg2Index)>=0))
		{
			return true;
		}
	}
	if(relationshipIndex==5)
	{
		if((binarySearch(arg1ParentT,0,(arg1ParentT.size()),arg1Index)>=0) && (binarySearch(arg2ParentT,0,(arg2ParentT.size()),arg2Index)>=0))
		{
			return true;
		}
	}
	if(relationshipIndex==6)
	{
		if((binarySearch(arg1Call,0,(arg1Call.size()),arg1Index)>=0) && (binarySearch(arg2Call,0,(arg2Call.size()),arg2Index)>=0))
		{
			return true;
		}
	}
	if(relationshipIndex==7)
	{
		if((binarySearch(arg1CallT,0,(arg1CallT.size()),arg1Index)>=0) && (binarySearch(arg2CallT,0,(arg2CallT.size()),arg2Index)>=0))
		{
			return true;
		}
	}
	if(relationshipIndex==8)
	{
		if((binarySearch(arg1Next,0,(arg1Next.size()),arg1Index)>=0) && (binarySearch(arg2Next,0,(arg2Next.size()),arg2Index)>=0))
		{
			return true;
		}
	}
	if(relationshipIndex==9)
	{
		if((binarySearch(arg1NextT,0,(arg1NextT.size()),arg1Index)>=0) && (binarySearch(arg2NextT,0,(arg2NextT.size()),arg2Index)>=0))
		{
			return true;
		}
	}
	return false;
};

int Query_Preprocessor::binarySearch(vector<int> argumentsList, int first, int last,int key) {

	while (first <= last) {
		int mid = (first + last) / 2;  // compute mid point.
		if (key > argumentsList[mid]) 
			first = mid + 1;  // repeat search in top half.
		else if (key < argumentsList[mid]) 
			last = mid - 1; // repeat search in bottom half.
		else
			return mid;     // found it. return position /////
	}
	return -(first + 1);    // failed to find key
};

/*int Query_Preprocessor::createQueryTree(string s,vector<suchThatClause>& suchThat,vector<queryResult>& result,vector<withClause>& with)
{
queryResult tempResult;
suchThatClause tempSuchThat;
withClause tempWith;
int relationshipIndex;
int entityIndex;
char * tmp ;
char *str;
int noOfCompoundQueries = 0;
tmp = new char [s.size()+1];
strcpy(tmp,s.c_str());
str = strtok(tmp," (),");

str = strtok(NULL," (),");
string entity = str;
int entityType = -1;
entityIndex = getEntityIndex(entity,entityType);

tempResult.resultType = entityIndex;
result.push_back(tempResult);
//insertNode(RESULT,-1,CHILD);
//insertNode(RESULT,entityIndex,CHILD);
//curr = root;

str = strtok(NULL," (),");
while(str!=NULL)
{
if(strcmp(str,"such")==0)
{

//insertNode(SUCHTHAT,-1,CHILD);
str = strtok(NULL," (),");
str = strtok(NULL," (),");
string relationship = str;
relationshipIndex = getRelationshipIndex(relationship);
tempSuchThat.relationshipType = relationshipIndex;
//insertNode(RELATIONSHIP,relationshipIndex,CHILD);
str = strtok(NULL," (),");
entity = str;
entityIndex = getEntityIndex(entity,entityType);
tempSuchThat.argument1 = entityIndex;
tempSuchThat.argument1Type = entityType;
//insertNode(entityType,entityIndex,CHILD);
str = strtok(NULL," (),");
entity = str;
entityType = -1;
entityIndex = getEntityIndex(entity,entityType);
tempSuchThat.argument2 = entityIndex;
tempSuchThat.argument2Type = entityType;
tempSuchThat.index = 0;
suchThat.push_back(tempSuchThat);

//insertNode(entityType,entityIndex,SIBLING);


}
str = strtok(NULL," (),");
noOfCompoundQueries++;
}
return 1;
};*/

int Query_Preprocessor::createQueryTree(string s,vector<suchThatClause>& suchThat,struct Result& result,vector<withClause>& with)
{
	suchThat.clear();
	//result.clear();
	with.clear();
	Result tempResult;
	suchThatClause tempSuchThat;
	withClause tempWith;
	Argument tempArgument;
	int relationshipIndex;
	int entityIndex;
	char * tmp ;
	char *str;
	int noOfCompoundQueries = 0;
	int curr = -1;
	string queryArgument = "";
	tmp = new char [s.size()+1];
	strcpy(tmp,s.c_str());
	str = strtok(tmp," ");
	queryArgument = str;
	

	if(boost::iequals(queryArgument,"select")!=1)
	{
		cout<<"Invalid Query Format: Use Select (design-entity) clauses";
		return -1;
	}
		str = strtok(NULL," ");
		string entity = str;
		int entityType = -1;
		int argInd = 0;
		if(boost::iequals(str,"boolean")==1)
		{
		entityIndex = _BOOLEAN;
		}
		else
		{
			entityIndex = getEntityIndex(entity,entityType,argInd);
		}
		if(entityIndex<0)
		{
			cout<<"Invalid entity type. Please check if you have declared the above entity";
			return -1;
		}
		result.selectedType = (QRYSTATEMENT_TYPE)entityIndex;
		if(entityIndex!=_BOOLEAN)
		{
		result.resultIndex = argInd;
		}	
			str = strtok(NULL," ");
			while(str!=NULL)
			{
				if((boost::iequals(str,"such")==1)||((boost::iequals(str,"and")==1)&&(curr==SUCHTHAT)))
				{

					if(boost::iequals(str,"such")==1)
					{
					str = strtok(NULL," ");
					if(boost::iequals(str,"that")!=1)
					{
					cout<<"Invalid clause: Expected \"such that\"";
						return -1;
					}
					}
					
					curr = SUCHTHAT;
						tempSuchThat.arguments.clear();
						str = strtok(NULL," ");
						entity = str;
						vector<string> tokens;
						tokenize(tokens,entity,"(");
						string relationship = tokens.front();
						relationshipIndex = getRelationshipIndex(relationship);
						if(relationshipIndex<0)
						{
							cout<<"Invalid relationship type.";
							return -1;
						}
							tempSuchThat.relationshipType = relationshipIndex;
							
							if(tokens.size()<2)
							{
								cout<<"Invalid arguments format. Please use [relationship](arg1, arg2)";
								return -1;
							}
							entity = tokens[1];
							
							if(entity[entity.size()-1]!=',')
							{
								cout<<"Invalid arguments format. Please use [relationship](arg1, arg2)"; 
								return -1;
							}
							
								string tempArg1(entity.begin(),entity.end()-1);
								int argInd = -1;
								entityIndex = getEntityIndex(tempArg1,entityType,argInd);
								
								if(entityIndex<0)
								{
									if(entityIndex==-1)
									{
									cout<<"Invalid entity type. Please check if you have declared the entity "<<tempArg1;
									}
									else
									{
										return -2;
									}
									return -1;
								}
								if(entityType==INTARGUMENT)
								{
									int intValidity = checkIntArgument(entityIndex,_STMT);
									if(intValidity<0)
									{
									return -2;
									}
								}
								tempArgument.argument = entityIndex;
								tempArgument.argumentType = entityType;
								tempArgument.argumentInd = argInd;
								tempSuchThat.arguments.push_back(tempArgument);
									str = strtok(NULL," ");
									if(str==NULL)
								{
								cout<<"Invalid arguments format. Please use [relationship](arg1, arg2)";
								return -1;
								}
									entity = str;
									if(entity[entity.size()-1]!=')')
									{
										cout<<"Invalid arguments format. Please use [relationship](arg1, arg2)"; 
										return -1;
									}
										string tempArg2(entity.begin(),entity.end()-1);
										entityType = -1;
										argInd = -1;
										entityIndex = getEntityIndex(tempArg2,entityType,argInd);
										
										if(entityIndex<0)
										{
										if(entityIndex==-1)
										{
										cout<<"Invalid entity type. Please check if you have declared the entity "<< tempArg2;
										}
										else
										{
											return -2;
										}
										return -1;
										}
										if(entityType==INTARGUMENT)
										{
										int intValidity = checkIntArgument(entityIndex,_STMT);
										if(intValidity<0)
										{
										return -2;
										}
										}
											//arguments = entityIndex;
										tempArgument.argument = entityIndex;
										tempArgument.argumentType = entityType;
										tempArgument.argumentInd = argInd;
										tempSuchThat.arguments.push_back(tempArgument);
										int independence = checkIndependence(result.selectedType,result.resultIndex,tempSuchThat.arguments[0].argument,tempSuchThat.arguments[1].argument,tempSuchThat.arguments[0].argumentInd,tempSuchThat.arguments[1].argumentInd);
											tempSuchThat.index = independence;
											suchThat.push_back(tempSuchThat);
											int tempArgument1= tempSuchThat.arguments[0].argument;
											int tempArgument2= tempSuchThat.arguments[1].argument;
											if(tempSuchThat.arguments[0].argumentType==INTARGUMENT)
											{
												tempArgument1 = _INTEGER;
											}
											if(tempSuchThat.arguments[1].argumentType==INTARGUMENT)
											{
												tempArgument2 = _INTEGER;
											}
											if(tempSuchThat.arguments[0].argumentType==VARARGUMENT)
											{
												tempArgument1 = _VARIABLE;
											}
											if(tempSuchThat.arguments[1].argumentType==VARARGUMENT)
											{
												tempArgument2 = _VARIABLE;
											}
											if(tempSuchThat.arguments[0].argumentType==PROCARGUMENT)
											{
												tempArgument1 = _PROCEDURE;
											}
											if(tempSuchThat.arguments[1].argumentType==PROCARGUMENT)
											{
												tempArgument2 = _PROCEDURE;
											}
											
											if(!checkRelationshipArgumentsValidity(tempArgument1,tempArgument2,relationshipIndex))
											{
												cout<<"Invalid arguments. Please ensure the arguments match the relationship "<<RelationshipTable[relationshipIndex];
											return -1;
											}
											
								
					
						
					
							}

				else if((boost::iequals(str,"with")==1)||((boost::iequals(str,"and")==1)&&(curr==WITH)))
				{
					curr = WITH;
					tempWith.arguments.clear();
					str = strtok(NULL," ");
					
							if(str==NULL)
							{
								cout<<"Invalid with type. Expected Arguments";
								return -1;
							}
							entity = str;
					/*		char *tmpAttrCondition; 
							char *tempString = new char[entity.size()];
							strcpy(tempString,entity.c_str());
							tmpAttrCondition = strtok(tempString,".");
							entity = tmpAttrCondition;*/
							int argInd = -1;
							vector<string> tokens;
							tokenize(tokens,entity,".");
							entity = tokens.front();
							entityIndex = getEntityIndex(entity,entityType,argInd);
							
							if(entityIndex<0)
							{
								if(entityIndex==-1||entityType!=ARGUMENT)
									{
									cout<<"Invalid entity type. Please check if you have declared the entity "<<entity;
									}
							 return -1;
							}
									tempArgument.argument = entityIndex;
									
									if(tokens.size()<2)
									{
										return -1;
									}
									string tmpAttrCondition = tokens[1];
									
									int result = checkAttributeName(entityIndex,tmpAttrCondition);
									if(result<0)
									{
										return -1;
									}
									tempArgument.argumentType = entityType;
									tempArgument.argumentInd = argInd;
									str = strtok(NULL," ");
									if(strcmp(str,"=")!=0)
									{
										cout<<"Invalid Comparison: Expected \"=\"";
										return -1;
									}
									str = strtok(NULL," ");
									entity = str;
									tokens.clear();
									tokenize(tokens,entity,".");
							entity = tokens.front();
							argInd = -1;
							entityIndex = getEntityIndex(entity,entityType,argInd);
							if(entityIndex<0)
							{
								if(entityIndex==-1)
									{
									cout<<"Invalid entity type. Please check if you have declared the entity "<<entity;
									}
								else
								{
									return -2;
								}
							 return -1;
							}
							if(entityType==INTARGUMENT)
								{
									int intValidity = checkIntArgument(entityIndex,tempArgument.argument);
									if(intValidity<0)
									{
									return -2;
									}
								}
									tempWith.arguments.push_back(tempArgument);
									tempArgument.argument = entityIndex;
									tempWith.withType = _VALUE;
									
									if(tokens.size()>1)
									{
									tmpAttrCondition = tokens[1];
									int result = checkAttributeName(entityIndex,tmpAttrCondition);
									if(result<0)
									{
										cout<<"Invalid Attribute. Please use the approriate attribute and attribute Name";
										return -1;
									}
									tempWith.withType = _COMPARISON;
									}
									tempArgument.argumentType = entityType;
									tempArgument.argumentInd = argInd;
									tempWith.arguments.push_back(tempArgument);
									result = checkAttributeCompatibility(tempWith.arguments[0].argument,tempWith.arguments[0].argument);
									if(result<0)
									{
										cout<<"Invalid attribute comparison. Please use attributes of the same type";
										return -1;
									}
									with.push_back(tempWith);		
								}
						else 
						{
							cout<<"Invalid clause";
							return -1;
						}
					
				str = strtok(NULL," ");
				noOfCompoundQueries++;
			}
		
	return 1;
};
int Query_Preprocessor::insertNode(int nodeType,int queryData,int linkType)
{
	QueryNode* node = new QueryNode();
	node->entityType = nodeType;
	node->data = queryData;

	if(linkType==CHILD)
	{
		node->parent = curr;
		node->level = curr->level + 1;
		curr->children.push_back(node);
		curr = node;
		return 1;
	}

	else
	{
		node->parent = curr->parent;
		node->level = curr->level;
		node->parent->children.push_back(node);
		return 1;
	}
	return -1;
};


int Query_Preprocessor::getRelationshipIndex(string Relationship)
{

	for(int i =0;i<RelationshipTable.size();i++)
	{
		if(RelationshipTable[i].compare(Relationship)==0)
			return i;
	}
	return -1;


};
int Query_Preprocessor::getEntityIndex(string entity,int &entityType,int &entityInd)
{
	int Number = 0;
	vector<string> tempName;
	if(entity[0]=='\"')
	{
		tokenize(tempName,entity,"\"");
		Number = queryObj.getEntityTypeIndex(tempName.front(),entityType);
		if(Number>=0)
		{
		return Number;
		}
		else
		{
			return -2;
		}
	}
	else
	{
		if((Number=isNumber(entity))<0)
		{
			for(int i =0;i<entityTableMapping.size();i++)
			{
				for(int j=0;j<entityTableMapping[i].size();j++)
				{
					if(entityTableMapping[i][j].compare(entity)==0)
					{
						entityType = ARGUMENT;
						entityInd = j;
						
						return i;
					}
				}
			}
		}
		else
		{
			entityType = INTARGUMENT;
			
			return  Number;
			
		}
	}
	return -1;


};

int Query_Preprocessor::isNumber(string temp)
{
	int returnValue = -1;
	if(temp.compare("0")==0)
	{
		return 0;
	}
	for( int i = 0; i < temp.length(); i++) {
		
			if(isdigit(temp[i]))
			{
				returnValue = 1;
			}
			else
			{
				returnValue = -1;
				break;
			}
		
	}
	if(returnValue==1)
	{
	returnValue =  atoi(&temp[0]);
	
	}
	return returnValue;
};
int Query_Preprocessor::checkIntArgument(int argument, int argType)
{
int result = queryObj.validateEntity(argument,argType);

return result;
};





int Query_Preprocessor::checkAttributeName(int attrIndex,string atrName)
{
	if(attrName[attrIndex].compare(atrName)==0)
	{
		return 1;
	}
	else
	{
		return -1;
	}

}
int Query_Preprocessor::tokenize(vector<string>& tokens,const string& str,const string& delimiters)

{
	
    	
	// skip delimiters at beginning.
    	string::size_type lastPos = str.find_first_not_of(delimiters, 0);
    	
	// find first "non-delimiter".
    	string::size_type pos = str.find_first_of(delimiters, lastPos);

    	while (string::npos != pos || string::npos != lastPos)
    	{
        	// found a token, add it to the vector.
        	tokens.push_back(str.substr(lastPos, pos - lastPos));
		
        	// skip delimiters.  Note the "not_of"
        	lastPos = str.find_first_not_of(delimiters, pos);
		
        	// find next "non-delimiter"
        	pos = str.find_first_of(delimiters, lastPos);
    	}

	return 1;
};

int Query_Preprocessor::checkAttributeCompatibility(int arg1,int arg2)
{
	if(arg1==arg2)
	{
		return 1;
	}
	if((arg1==_PROCEDURE && arg2==_VARIABLE) ||(arg2==_PROCEDURE && arg1==_VARIABLE))
	{
		return 1;
	}
	if((arg1==_STMT && arg2==_CONSTANT)||(arg2==_STMT && arg1==_CONSTANT))
	{
		return 1;
	}
	return -1;



};
int Query_Preprocessor::checkIndependence(int resultType,int resultInd, int argument1,int argument2,int argInd1,int argInd2)
{

	if(argument1==resultType)
	{
		if(argInd1==resultInd)
		{
			return -1;
		}
	}
		
	if(argument2==resultType)
	{
		if(argInd2==resultInd)
		{
			return -1;
		}
	}
	
	return 1;

};
